package ae.pcfc.cldos.online.web.services.breadcrumbs.beans;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.services.breadcrumbs.BreadCrumb;
import ae.pcfc.cldos.online.web.services.breadcrumbs.BreadCrumbsFactoryService;
import ae.pcfc.cldos.online.web.services.breadcrumbs.provider.BreadCrumbImpl;

/**
 * 
 * @author arosa
 *
 */
@Service
public class BreadCrumbsFactoryServiceImpl implements BreadCrumbsFactoryService {

	
	private static final String BREADCRUMB_MODEL_NAME = "breadcrumb";
	

	
	private BreadCrumb noBreadCrumb = 
			BreadCrumbImpl.createBaseBreadCrumb(createUnclicableMessageDrivenBreadCrumb("breadcrumb.no"));
	
	public void populateNoBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, noBreadCrumb.getBreadCrumb());
	}
	/**
	 * 
	 */
	private BreadCrumb allAppReqBreadCrumb = 
		BreadCrumbImpl.createBaseBreadCrumb(createClicableMessageDrivenBreadCrumb("menu.label.all_application_requests", ViewPath.__ALL_APP_REQUESTS+"/show.htm"));
	/**
	 * @param model
	 */
	public void populateAllAppRequestsBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, allAppReqBreadCrumb.getBreadCrumb());
	}
	/**
	 * Fin services
	 */

	private BreadCrumb finServicesBreadCrumb = 
		BreadCrumbImpl.createBaseBreadCrumb(createClicableMessageDrivenBreadCrumb("label.fin_services", ViewPath.__ADMIN_FINANCE_SERVICES+"/show.htm?appId=0"));

	/**
	 * @param model
	 */
	public void populateFinServicesBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, finServicesBreadCrumb.getBreadCrumb());
	}
	/**
	 * Main Page
	 */
	private BreadCrumb mainPageBreadCrumb = 
			BreadCrumbImpl.createBaseBreadCrumb(createClicableMessageDrivenBreadCrumb("breadcrumb.main.page", ViewPath.__HOME+"/show.htm"));
	
	public void populateMainPageBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, mainPageBreadCrumb.getBreadCrumb());
	}
	
	/**
	 *  Inbox
	 */
	private BreadCrumb inboxBreadCrumb = 
			BreadCrumbImpl.createBreadCrumb(createUnclicableMessageDrivenBreadCrumb("breadcrumb.inbox"), mainPageBreadCrumb);
	
	public void populateInboxBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, inboxBreadCrumb.getBreadCrumb());
	}
		
	public void populateFinancialData(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, financialData.getBreadCrumb());
	}
	
	public void populateGeneralData(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, generalData.getBreadCrumb());
	}
	/**
	 * notifications
	 */
	private BreadCrumb notificationBreadCrumb = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("breadcrumb.notifications", ViewPath.__ADMIN_NOTIFICTIONS+"/show.htm"), inboxBreadCrumb);
	
	private BreadCrumb notificationDtlBreadCrumb = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("breadcrumb.notificationdetails", ViewPath.__ADMIN_NOTIFICTIONS_DETAILS+"/view.htm"), notificationBreadCrumb);
	
	public void populateNotificationsDtlBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, notificationDtlBreadCrumb.getBreadCrumb());
	}
	
	public void populateNotificationsBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, notificationBreadCrumb.getBreadCrumb());
	}
	public void populateallAppReqDtlBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, allAppReqDtlBreadCrumb.getBreadCrumb());
	}
	private BreadCrumb allAppReqDtlBreadCrumb = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("breadcrumb.allappreqdetails", ViewPath.__ADMIN_EMPLOYMENT_VISA_VIEW+"/form.htm"), allAppReqBreadCrumb);
	
	/**
	 * tasks
	 */
	private BreadCrumb tasksBreadCrumb = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("breadcrumb.tasks", 
					ViewPath.__ADMIN_TASKS+"/show.htm"), inboxBreadCrumb);
	
	public void populateTasksBreadCrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, tasksBreadCrumb.getBreadCrumb());
	}
		
	private BreadCrumb currentTaskBreadcrumb = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb(null, 
					ViewPath.__TASK+"?appId=%s"), tasksBreadCrumb);
	
	public void populateMainAdminRewiewBreadcrumb(ModelMap model, String appId, Integer tasktype){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(tasktype.equals(TASK_TYPE_MAIN_ADMIN_APPROVE)){
			messageCode = "main.admin.review.breadcrumb";
		} else if(tasktype.equals(TASK_TYPE_RESUBMIT)) {
			messageCode = "main.resubmit.breadcrumb";	
		}
		
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
		
	private BreadCrumb govermentOnlineServices = 
			BreadCrumbImpl.createBreadCrumb(createUnclicableMessageDrivenBreadCrumb("menu.label.goverment_online_services"), 
					mainPageBreadCrumb);
	
	private BreadCrumb employmentVisa = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("menu.label.employment_visa_outside",
					ViewPath.__COMPANY_EMPLOYMENT_VISA_REGISTRATION+"/form.htm?"+ Constants.__EMP_VISA_TYPE_PARAM + "=employment&"+
							Constants.__EMP_SOURCE_PARAM+"=%s"), govermentOnlineServices);
	
	private BreadCrumb visitVisa = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("menu.label.visit_visa_one_month",
					ViewPath.__COMPANY_VISIT_VISA_REGISTRATION+"/form.htm?"+ Constants.__EMP_VISA_TYPE_PARAM + "=visit&"+
							Constants.__EMP_SOURCE_PARAM+"=%s"), govermentOnlineServices);
	
	private BreadCrumb employmentVisaRenewal = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("emp.visa.renewal.breadcrumb",
					ViewPath.__COMPANY_EMPLOYMENT_VISA_REGISTRATION+"/form.htm"), govermentOnlineServices);
	
	private BreadCrumb nocAndLetters = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("noc.and.letters.breadcrumb",
					ViewPath.__NOC_LETTERS_SUBMITION+"/form.htm"), govermentOnlineServices);
	
	private BreadCrumb employmentVisaTermination = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("emp.visa.term.breadcrumb",
					ViewPath.__EV_TERMINATION+"/form.htm"), govermentOnlineServices);
	
	private BreadCrumb visitVisaTermination = 
			BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("visit.visa.term.breadcrumb",
					ViewPath.__VV_CANCEL+"/form.htm"), govermentOnlineServices);
	
	/**
	 * Company Details
	 */
	private BreadCrumb companyDetails = 
		BreadCrumbImpl.createBreadCrumb(createUnclicableMessageDrivenBreadCrumb("menu.label.company_details"), 
				mainPageBreadCrumb);
	
	/**
	 * General Comapany Info
	 */
	private BreadCrumb financialData = 
		BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("header.fininfo", 
				ViewPath.__COMPANY_FINANCIAL_INFO+"/show.htm"), companyDetails);
	
	/**
	 * Financial Company Data
	 */
	private BreadCrumb generalData = 
		BreadCrumbImpl.createBreadCrumb(createClicableMessageDrivenBreadCrumb("header.geninfo", 
				ViewPath.__COMPANY_GENERAL_INFO+"/show.htm"), companyDetails);

	public void populateEmploymentVisaBreadCrumb(ModelMap model, Integer empSource){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(employmentVisa.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(Constants.__EMPLOYEE_SOURCE_OUTSIDE.equals(empSource)){
			messageCode = "menu.label.employment_visa_outside";
		}else if (Constants.__EMPLOYEE_SOURCE_INSIDE.equals(empSource)){
			messageCode = "menu.label.employment_visa_inside";
		}else if (Constants.__EMPLOYEE_SOURCE_GOVERMENT.equals(empSource)){
			messageCode = "menu.label.transfer_sponsorship";
		}else if (Constants.__EMPLOYEE_SOURCE_INTERNAL.equals(empSource)){
			messageCode = "menu.label.internal_transfer";
		}
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, String.format(last.getUrl(), empSource));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	public void populateVisitVisaBreadCrumb(ModelMap model, Integer empSource){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(visitVisa.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(Constants.__VISIT_VISA_ONE_MONTH.equals(empSource)){
			messageCode = "menu.label.visit_visa_one_month";
		}else if (Constants.__VISIT_VISA_THREE_MONTH.equals(empSource)){
			messageCode = "menu.label.visit_visa_three_months";
		}
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, String.format(last.getUrl(), empSource));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	
	public void populateEmploymentVisaRenewalBreadcrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, employmentVisaRenewal.getBreadCrumb());
	}
	
	public void populateEmploymentVisaTermBreadcrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, employmentVisaTermination.getBreadCrumb());
	}

	public void populateVisitVisaTermBreadcrumb(ModelMap model){
		model.put(BREADCRUMB_MODEL_NAME, visitVisaTermination.getBreadCrumb());
	}
	public void populateEmploymentVisaRenewalTasktBreadcrumb(ModelMap model, String appId, Integer taskType){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(taskType.equals(TASK_TYPE_MAIN_ADMIN_APPROVE)){
			messageCode = "emp.visa.renewal.review.breadcrumb";
		} else if(taskType.equals(TASK_TYPE_RESUBMIT)) {
			messageCode = "emp.visa.renewal.resubmit.breadcrumb";	
		}
		
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	public void populateEmploymentVisaTerminationTasktBreadcrumb(ModelMap model, String appId, Integer taskType){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(taskType.equals(TASK_TYPE_MAIN_ADMIN_APPROVE)){
			messageCode = "emp.visa.term.review.breadcrumb";
		} else if(taskType.equals(TASK_TYPE_RESUBMIT)) {
			messageCode = "emp.visa.term.resubmit.breadcrumb";	
		}
		
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	public void populateVisitVisaCancelTasktBreadcrumb(ModelMap model, String appId, Integer taskType){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(taskType.equals(TASK_TYPE_MAIN_ADMIN_APPROVE)){
			messageCode = "visit.visa.term.review.breadcrumb";
		} else if(taskType.equals(TASK_TYPE_RESUBMIT)) {
			messageCode = "visit.visa.term.resubmit.breadcrumb";	
		}
		
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	public void populateEPPaymentBreadcrumb(ModelMap model, String appId){
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = "ep.payment.breadcrumb.message";
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
	}
	
	/////--------------------------------------------------------------------------------
	
	public void populateNocAndLettersBreadcrumb(ModelMap model) {
		model.put(BREADCRUMB_MODEL_NAME, nocAndLetters.getBreadCrumb());
	}

/*	
	List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(employmentVisa.getBreadCrumb());
	BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
	String messageCode = null;
	if(Constants.__EMPLOYEE_SOURCE_OUTSIDE.equals(empSource)){
		messageCode = "menu.label.employment_visa_outside";
	}else if (Constants.__EMPLOYEE_SOURCE_INSIDE.equals(empSource)){
		messageCode = "menu.label.employment_visa_inside";
	}else if (Constants.__EMPLOYEE_SOURCE_GOVERMENT.equals(empSource)){
		messageCode = "menu.label.transfer_sponsorship";
	}else if (Constants.__EMPLOYEE_SOURCE_INTERNAL.equals(empSource)){
		messageCode = "menu.label.internal_transfer";
	}
	BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, String.format(last.getUrl(), empSource));
	breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
	model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
*/	
	
	public void populateNocAndLettersTasktBreadcrumb(ModelMap model, String appId, Integer taskType) {
		List<BreadCrumbBean> breadCrumbBeans = new ArrayList<BreadCrumbBean>(currentTaskBreadcrumb.getBreadCrumb());
		BreadCrumbBean last = breadCrumbBeans.get(breadCrumbBeans.size()-1);
		String messageCode = null;
		if(taskType.equals(TASK_TYPE_MAIN_ADMIN_APPROVE)){
			messageCode = "noc.and.letters.review.breadcrumb";
		} else if(taskType.equals(TASK_TYPE_RESUBMIT)) {
			messageCode = "noc.and.letters.resubmit.breadcrumb";	
		}
		
		BreadCrumbBean current = createClicableMessageDrivenBreadCrumb(messageCode, 
				String.format(last.getUrl(), appId));
		breadCrumbBeans.set(breadCrumbBeans.size()-1, current);
		model.put(BREADCRUMB_MODEL_NAME, breadCrumbBeans);
		
		/// emp.visa.renewal.resubmit.breadcrumb
		/// emp.visa.renewal.review.breadcrumb
		
///		model.put(BREADCRUMB_MODEL_NAME, employmentVisaTermination.getBreadCrumb());
	}
	/////--------------------------------------------------------------------------------
	
//	private BreadCrumbBean createClicableBreadCrumb(String nameEn, String nameAr, String url){
//		return new BreadCrumbBean(nameEn, nameAr, url, true, false);
//	}
//	
//	private BreadCrumbBean createUnclicableBreadCrumb(String nameEn, String nameAr){
//		return new BreadCrumbBean(nameEn, nameAr, null, false, false);
//	}
	
	private BreadCrumbBean createClicableMessageDrivenBreadCrumb(String messageCode, String url){
		return new BreadCrumbBean(messageCode, null, url, true, true);
	}
	
	private BreadCrumbBean createUnclicableMessageDrivenBreadCrumb(String messageCode){
		return new BreadCrumbBean(messageCode, null, null, false, true);
	}
	
}
